<%@page import="com.healtyprj.Constants"%>
<%@page import="com.amazonaws.services.dynamodb.datamodeling.DynamoDBScanExpression"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.amazonaws.util.json.JSONArray"%>
<%@page import="java.util.List"%>
<%@page import="com.amazonaws.services.dynamodb.datamodeling.DynamoDBQueryExpression"%>
<%@page import="com.healtyprj.Goal"%>
<%@page import="com.healtyprj.User"%>
<%@page import="com.healtyprj.Weight"%>
<%@page import="com.amazonaws.util.json.JSONObject"%>
<%@page import="org.springframework.util.StringUtils"%>
<%@page import="com.amazonaws.services.dynamodb.datamodeling.DynamoDBMapper"%>
<%@page import="com.amazonaws.services.dynamodb.model.PutItemResult"%>
<%@page import="com.amazonaws.services.dynamodb.model.PutItemRequest"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page import="com.amazonaws.services.dynamodb.model.AttributeValue"%>
<%@page import="com.amazonaws.AmazonServiceException"%>
<%@page import="com.amazonaws.services.dynamodb.model.TableStatus"%>
<%@page import="com.amazonaws.services.dynamodb.model.DescribeTableRequest"%>
<%@page import="com.amazonaws.services.dynamodb.model.TableDescription"%>
<%@page import="com.amazonaws.services.dynamodb.model.ProvisionedThroughput"%>
<%@page import="com.amazonaws.services.dynamodb.model.KeySchemaElement"%>
<%@page import="com.amazonaws.services.dynamodb.model.KeySchema"%>
<%@page import="com.amazonaws.services.dynamodb.model.CreateTableRequest"%>
<%@page import="com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider"%>
<%@page import="com.amazonaws.services.dynamodb.AmazonDynamoDBClient"%>
<%@page import="com.amazonaws.services.dynamodb.AmazonDynamoDB"%>
<%@page import="com.amazonaws.services.dynamodb.model.Condition"%>
<%@page import="com.amazonaws.services.dynamodb.model.ComparisonOperator"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/x-json; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(new ClasspathPropertiesFileCredentialsProvider());
dynamoDB.setEndpoint(Constants.REGION);

DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDB);
//ハッシュキーを設定
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
Condition rangeKeyCondition = new Condition()
	.withComparisonOperator(ComparisonOperator.GE.toString())
	.withAttributeValueList(new AttributeValue().withS(format.format(new Date())));
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
scanExpression.addFilterCondition("finishDate", rangeKeyCondition);

List<Goal> goals = dynamoDBMapper.scan(Goal.class, scanExpression);
JSONArray jsons = new JSONArray();
for (Goal goal : goals) {
	JSONObject json = new JSONObject();
	json.put("name", goal.getName());
	json.put("finishDate", goal.getFinishDate());
	json.put("goalWeight", goal.getWeight());
	
	Condition recentWidthCondition = new Condition()
	.withComparisonOperator(ComparisonOperator.GE.toString())
	.withAttributeValueList(new AttributeValue().withS(goal.getStartDate()));
	DynamoDBQueryExpression recentQuery = new DynamoDBQueryExpression(
			new AttributeValue().withS(goal.getName()));
	recentQuery.setRangeKeyCondition(recentWidthCondition);
	List<Weight> weights = dynamoDBMapper.query(Weight.class, recentQuery);
	if (!weights.isEmpty()) {
		json.put("nowWeight", weights.get(weights.size() - 1).getWeight());
	} else {
		json.put("nowWeight", "0");
	}
	jsons.put(json);
}
out.print(jsons.toString());
%>